//
//  LBPopupPresentationController.m
//  TEXT
//
//  Created by mac on 2024/7/28.
//  Copyright © 2024 刘博. All rights reserved.
//

#import "LBPopupPresentationController.h"
#import "LBPopBaseViewController.h"
#import "LBFunctionTestHeader.h"

@implementation LBPopupPresentationController

- (instancetype)initWithPresentedViewController:(UIViewController *)presentedViewController presentingViewController:(UIViewController *)presentingViewController
{
    if (self = [super initWithPresentedViewController:presentedViewController presentingViewController:presentingViewController]) {
        presentedViewController.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}

- (LBPopBaseViewController *)getPopupViewController {
    LBPopBaseViewController *vc = [self getPopupViewController:self.presentingViewController];
    if (!vc) {
        vc = [self getPopupViewController:self.presentedViewController];
    }
    return vc;
}

- (LBPopBaseViewController *)getPopupViewController:(UIViewController *)vc {
    UINavigationController *nav = (UINavigationController *)vc;
    if ([nav isKindOfClass:[UINavigationController class]]) {
        LBPopBaseViewController *vc = [nav.viewControllers firstObject];
        if ([vc isKindOfClass:[LBPopBaseViewController class]]) {
            return vc;
        }
    }
    return nil;
}

/**
 present将要执行
 */
- (void)presentationTransitionWillBegin {
    NSLog(@"");
    LBPopBaseViewController *vc = [self getPopupViewController];
    UIView *viewContent = vc.viewContent;
    UIView *viewDismiss = vc.viewDismiss;
    viewContent.zf_top = viewContent.superview.zf_height;   // 初始化
    viewDismiss.alpha = 0.f;
}

/**
 present执行结束
 */
- (void)presentationTransitionDidEnd:(BOOL)completed {
}

/**
 dismiss将要执行
 */
- (void)dismissalTransitionWillBegin {
}

/**
 dismiss执行结束
 */
- (void)dismissalTransitionDidEnd:(BOOL)completed {
    NSLog(@"completed:%d", completed);
}

#pragma mark - UIViewControllerAnimatedTransitioning

- (NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext {
    return 0.3;   // 动画时长
}

- (void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext {
    UIView *containerView = [transitionContext containerView];
    UIViewController *fromVc = [transitionContext viewControllerForKey:UITransitionContextFromViewControllerKey];
    UIViewController *toVc = [transitionContext viewControllerForKey:UITransitionContextToViewControllerKey];
    UIView *fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    UIView *toView = [transitionContext viewForKey:UITransitionContextToViewKey];
    [containerView addSubview:toView];
    
    LBPopBaseViewController *vc = [self getPopupViewController];
    UIView *viewContent = vc.viewContent;
    UIView *viewDismiss = vc.viewDismiss;
    
    BOOL isShow = YES;
    
    NSLog(@"isPresent:%d height:%f viewComment:%@", toVc.presentingViewController == fromVc, toView.zf_height, viewContent);
    if (toVc.presentingViewController == fromVc) {   // 显示
//        toView.top = viewComment ? viewComment.height : containerView.height;
        toView.frame = containerView.bounds;
        [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:2.0 initialSpringVelocity:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            viewDismiss.alpha = 1.f;
            viewContent.zf_bottom = toView.zf_height;
//            toView.bottom = containerView.height;
        } completion:^(BOOL finished) {
            BOOL cancel = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!cancel];
        }];
    } else {   // 隐藏
//        fromView.bottom = containerView.height;
        isShow = NO;
        [UIView animateWithDuration:[self transitionDuration:transitionContext] delay:0.0 usingSpringWithDamping:2.0 initialSpringVelocity:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            viewContent.zf_top = fromView.zf_height;
            viewDismiss.alpha = 0.f;
//            fromView.top = viewComment ? viewComment.height : containerView.height;
        } completion:^(BOOL finished) {
            if ([fromVc isKindOfClass:[UINavigationController class]]) {
                [((UINavigationController*)fromVc) popViewControllerAnimated:NO];   // 推出VC，否则会有内存泄漏
            }
            BOOL cancel = [transitionContext transitionWasCancelled];
            [transitionContext completeTransition:!cancel];
        }];
    }
    
    if (self.didScrolldelegate && [self.didScrolldelegate respondsToSelector:@selector(presentView:didIsShow:)]) {
        [self.didScrolldelegate presentView:self didIsShow:isShow];
    }
}
#pragma mark - UIViewControllerTransitioningDelegate

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source {
    return self;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed {
    return self;
}

- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source {
    NSAssert(self.presentedViewController == presented, @"You didn't initialize %@ with the correct presentedViewController.  Expected %@, got %@.",
             self, presented, self.presentedViewController);
    return self;
}


@end
